package com.xxx.reggie.controller;

import com.xxx.reggie.common.R;
import com.xxx.reggie.entity.Category;
import com.xxx.reggie.entity.Employee;
import com.xxx.reggie.entity.Page;
import com.xxx.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    //注入CategoryService
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category, HttpSession session){

        //获取登入人信息
        Employee employee = (Employee) session.getAttribute("employee");

        //完善Category信息
        //创建人id
        category.setCreateUser(employee.getId());
        //修改人id
        category.setUpdateUser(employee.getId());

        //调用业务方法添加操作
        categoryService.save(category);

        //封装返回结果
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<Category>> findByPage(Integer page, Integer pageSize){
        //接收当前客户端请求的当前页面和每页大小
        Page<Category> byPage = categoryService.findByPage(page, pageSize);

        return R.success(byPage);
    }

    @DeleteMapping
    public R<String> deleteById(long id){
        //调用业务
        categoryService.deleteById(id);

        //返回结果
        return R.success("删除成功");
    }


    @PutMapping
    public R<String> updateCategory(@RequestBody Category category,HttpSession session){

        //获取登入的员工
        Employee employee = (Employee) session.getAttribute("employee");

        //完善信息(修改人)
        category.setUpdateUser(employee.getId());

        //调用业务修改方法
        categoryService.updateCategory(category);

        //封装返回结果
        return R.success("修改成功");
    }

    @GetMapping("list")
    public R<List<Category>> list(Integer type){
        List<Category> categoryList = categoryService.list(type);

        return R.success(categoryList);
    }
}
